package com.video.main_video.manager.handle.userInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.video.main_video.api.Api;
import com.video.main_video.common.staticData.StaticData;
import com.video.main_video.common.utils.ImgUtils;
import com.video.main_video.dao.entity.User;
import com.video.main_video.dao.entity.VideoInfo;
import com.video.main_video.dao.request.userInfo.UserInfoRequest;
import com.video.main_video.dao.response.userInfo.UserInfoResponse;
import com.video.main_video.manager.handle.ApiHandler;
import com.video.main_video.manager.mapper.UserMapper;
import org.jpedal.parser.image.ImageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;

@Api
public class UserInfoManager implements ApiHandler<UserInfoRequest, UserInfoResponse> {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserInfoResponse handler(UserInfoRequest userInfoRequest) {
        User userInfo = getUserInfo();
        UserInfoResponse res=new UserInfoResponse();
        res.setUser(userInfo);
        res.setCode(200);
        res.setMsg("succ");
        return res;
    }


    private User getUserInfo(){
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getId,StaticData.UserId);
        User user = userMapper.selectOne(qw);
        try {
            String images = ImgUtils.compressImageByFile(new File(user.getAvatar()));
            if (null==images){
                user.setAvatar("");
            }
            user.setAvatar("data:image/jpeg;base64,"+images);

        } catch (Exception e) {
            e.printStackTrace();
        }

        user.setPassword(null);
        user.setIcon(null);
        user.setId(null);
        return user;

    }
}
